package com.gwm.starter.oss.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import com.gwm.starter.oss.props.OssProperties;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.InputStream;

@Slf4j
@Data
public class GwmOssService {

    @Resource
    private OssProperties ossConfig;

    private OSS ossClient;

    private static final String SLASH = "/";

    public GwmOssService(OSS ossClient) {
        this.ossClient = ossClient;
    }

    public InputStream getInputStream(String objectKey) {

        GetObjectRequest getObjectRequest = new GetObjectRequest(ossConfig.getBucketName(), objectKey);

        OSSObject object = ossClient.getObject(getObjectRequest);

        return object.getObjectContent();
    }

    public InputStream getInputStream(String prefixDir, String objectKey) {

        GetObjectRequest getObjectRequest = new GetObjectRequest(ossConfig.getBucketName(), prefixDir + SLASH + objectKey);

        OSSObject object = ossClient.getObject(getObjectRequest);

        return object.getObjectContent();
    }

    public InputStream getInputStreamWithBucket(String bucketName, String prefixDir, String objectKey) {

        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, prefixDir + SLASH + objectKey);

        OSSObject object = ossClient.getObject(getObjectRequest);

        return object.getObjectContent();
    }

    public InputStream getInputStreamWithBucket(String bucketName, String objectKey) {

        GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectKey);

        OSSObject object = ossClient.getObject(getObjectRequest);

        return object.getObjectContent();
    }

    /**
     * 上传文件到 OSS 服务
     * @param objectKey  文件在桶中的全路径
     * @param input      上传的文件流
     */
    public void putObject(String objectKey, InputStream input) {
        String bucketName = ossConfig.getBucketName();
        PutObjectResult result = ossClient.putObject(bucketName, objectKey, input);
        log.info("\n - upload file to {} , {} \n result = {}", bucketName, objectKey, result);
    }

    public void putObjectWithBucket(String bucketName, String objectKey, InputStream input) {
        PutObjectResult result = ossClient.putObject(bucketName, objectKey, input);
        log.info("\n - upload file to {} , {} \n result = {}", bucketName, objectKey, result);
    }

    /**
     * 上传文件到 OSS 服务
     * @param prefixDir     文件在桶中的路径前缀
     * @param objectKey     文件在桶中的路径
     * @param input         上传的文件流
     */
    public void putObject(String prefixDir, String objectKey, InputStream input) {
        String bucketName = ossConfig.getBucketName();
        if (StringUtils.isEmpty(prefixDir)) {
            PutObjectResult result = ossClient.putObject(bucketName, objectKey, input);
            log.info("\n - upload file to {} , {} \n result = {}", bucketName, objectKey, result);
        } else {
            PutObjectResult result = ossClient.putObject(bucketName, prefixDir + SLASH + objectKey, input);
            log.info("\n - upload file to {} , {} \n result = {}", bucketName, prefixDir + SLASH + objectKey, result);
        }
    }

    public void putObjectWithBucket(String bucketName, String prefixDir, String objectKey, InputStream input) {
        if (StringUtils.isEmpty(prefixDir)) {
            PutObjectResult result = ossClient.putObject(bucketName, objectKey, input);
            log.info("\n - upload file to {} , {} \n result = {}", bucketName, objectKey, result);
        } else {
            PutObjectResult result = ossClient.putObject(bucketName, prefixDir + SLASH + objectKey, input);
            log.info("\n - upload file to {} , {} \n result = {}", bucketName, prefixDir + SLASH + objectKey, result);
        }
    }

    /**
     * 获取文件的预览 URL
     * @param objectKey     文件在桶中的路径
     * @return              文件的预览 URL
     */
    public String getPreviewUrl(String objectKey) {
        String bucketName = ossConfig.getBucketName();
        return ossConfig.getPreviewUrl() + SLASH + bucketName + SLASH + objectKey;
    }

    public String getPreviewUrlWithBucket(String bucketName, String objectKey) {
        return ossConfig.getPreviewUrl() + SLASH + bucketName + SLASH + objectKey;
    }

    /**
     * 获取文件的预览 URL
     * @param prefixDir     文件在桶中的路径前缀
     * @param objectKey     文件在桶中的路径
     * @return              文件的预览 URL
     */
    public String getPreviewUrl(String prefixDir, String objectKey) {
        String bucketName = ossConfig.getBucketName();
        return ossConfig.getPreviewUrl() + SLASH + bucketName + SLASH + prefixDir + SLASH + objectKey;
    }

    public String getPreviewUrlWithBucket(String bucketName, String prefixDir, String objectKey) {
        return ossConfig.getPreviewUrl() + SLASH + bucketName + SLASH  + prefixDir + SLASH + objectKey;
    }

    public boolean doesObjectExist(String objectKey) {
        String bucketName = ossConfig.getBucketName();
        return ossClient.doesObjectExist(bucketName, objectKey);
    }

    public boolean doesObjectExist(String prefixDir, String objectKey) {
        String bucketName = ossConfig.getBucketName();
        if (StringUtils.isEmpty(prefixDir)) {
            return ossClient.doesObjectExist(bucketName, objectKey);
        } else {
            return ossClient.doesObjectExist(bucketName, prefixDir + SLASH + objectKey);
        }

    }

    public boolean doesObjectExistWithBucket(String bucketName, String objectKey) {
        return ossClient.doesObjectExist(bucketName, objectKey);
    }

    public boolean doesObjectExistWithBucket(String bucketName, String prefixDir, String objectKey) {
        if (StringUtils.isEmpty(prefixDir)) {
            return ossClient.doesObjectExist(bucketName, objectKey);
        } else {
            return ossClient.doesObjectExist(bucketName, prefixDir + SLASH + objectKey);
        }

    }

}
